package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entify.Employee;
import com.itheima.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /***
     * 员工登录
     */
    @PostMapping("/login")
    public R login(@RequestBody Employee employee, HttpSession httpsession) {

        LambdaQueryWrapper<Employee> qW = new LambdaQueryWrapper<>();
        qW.eq(Employee::getUsername, employee.getUsername());
        Employee one = employeeService.getOne(qW);
        //判断用户名是否存在
        if (one == null) {//用户名不存在
            return R.error("用户名不存在");
        }
        //用户名存在，判断密码是否正确，并且给密码加密
        String rawPwd = employee.getPassword();
        String dbPwd = one.getPassword();
        String pwd = DigestUtils.md5DigestAsHex(rawPwd.getBytes());

        if (!dbPwd.equals(pwd)) {
            return R.error("密码错误");
        }
        //判断账号是否可用
        Integer status = one.getStatus();
        if (status == 0) {
            return R.error("员工已禁用");
        }

        httpsession.setAttribute("loginEmployee", one);
        one.setPassword(null);
        return R.success(one);
    }

    /***
     * 员工退出登录
     */
    @PostMapping("/logout")
    public R logout(HttpSession httpsession) {

        httpsession.removeAttribute("employee");

        return R.success("退出成功");
    }

    /**
     * 新增员工
     */
    @PostMapping
    public R addEmployee(@RequestBody Employee employee, HttpSession httpSession) {

        //获取密码并加密
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));

        //调用方法保存数据并返回
        boolean save = employeeService.save(employee);

        return save ? R.success("添加成功") : R.error("添加失败");
    }

    /**
     * 员工分页查询
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, String name) {
        //分页构造
        Page pageInfo = new Page(page, pageSize);

        //构建条件构造器
        LambdaQueryWrapper<Employee> qw = new LambdaQueryWrapper();

        //实现查询条件,name不为空的判断
        qw.like(StringUtils.isNotEmpty(name), Employee::getName, name);

        //排序条件
        qw.orderByDesc(Employee::getUpdateTime);

        //调用方法
        employeeService.page(pageInfo, qw);
        return R.success(pageInfo);
    }

    /**
     * 员工的禁用和启用,修改员工信息
     */
    @PutMapping
    public R<String> updateEmployee(@RequestBody Employee employee, HttpSession httpSession) {
        Employee emId = (Employee) httpSession.getAttribute("loginEmployee");
        Long id = emId.getId();

        //修改员工数据的更新时间
        employee.setUpdateTime(LocalDateTime.now());

        //修改员工数据的更新姓名
        employee.setUpdateUser(id);

        //调用业务层修改员工信息
        boolean update = employeeService.updateById(employee);

        //return信息
        return update?R.success("修改成功"):R.error("修改失败");
    }

    /**
     * 编辑员工
     */
    @GetMapping("/{id}")
    public R update(@PathVariable("id") long id) {
        Employee employee = employeeService.getById(id);
        if (employee != null) {
            return R.success(employee);
        }
        return R.error("没有查询到该员工信息");
    }

}













